<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" width="60%" >
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="任务方案" prop="planId" >
        <el-select
          v-model="formData.planId"
          placeholder="请选择任务方案"
          style="width: 220px"
          >
            <el-option
              v-for="item in planOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
      </el-form-item>
      <el-form-item label="选择日期" prop="date" >
        <el-date-picker
              v-model="formData.date"
              type="date"
              placeholder="开始日期"
              value-format="YYYY-MM-DD"  
            />
      </el-form-item>
    </el-form>
    <div class="preview-title">工序时间预览</div>
    <el-table
      :stripe="true"
      :show-overflow-tooltip="true"
      row-key="id"
    >
      <el-table-column label="工序名称" prop="processName" align="center" width="200" v-if="visibleColumns.processName" />
      <el-table-column label="计划开始时间" prop="startTime" align="center" width="200" v-if="visibleColumns.processName" />
      <el-table-column label="计划结束时间" prop="endTime" align="center" width="200" v-if="visibleColumns.processName"/>
    </el-table>
    <template #footer>
      <el-button @click="dialogVisible = false">取消</el-button>
      <el-button @click="saveSettings" type="primary" >保存</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">

import { ProductCategoryApi, ProductCategoryVO } from '@/api/erp/product/category'
import { CommonStatusEnum } from '@/utils/constants'
import { ElMessage } from 'element-plus'

/** ERP 产品分类 表单 */
defineOptions({ name: 'ProductCategoryForm' })

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('任务设置') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const planOptions = ref([
  { value: 'deadline', label: '交期倒算' },
  { value: 'time', label: '时间推算' }
])
const formData = ref({
  id: undefined,
  planId: undefined,
  date: undefined,
  code: undefined,
  sort: undefined,
  status: CommonStatusEnum.ENABLE
})
const formRules = reactive({
  planId: [{ required: true, message: '任务方案不能为空', trigger: 'blur' }],
  date: [{ required: true, message: '日期不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref
// const loading = ref(true) // 列表的加载中
// const list = ref([]) // 列表的数据

const visibleColumns = ref({
  processName:true,
  startTime:true,
  endTime:true
})
const saveSettings = async () => {
  await formRef.value.validate()
  try {
    formLoading.value = true
    // 这里执行实际的保存逻辑
    // await api.saveSettings(formData.value)
    
    // 模拟API调用延迟
    await new Promise(resolve => setTimeout(resolve, 1000))
    
    // 显示成功消息
    ElMessage.success('保存成功')
    
    // 关闭对话框
    dialogVisible.value = false
  } catch (error) {
    ElMessage.error('保存失败')
    console.error('保存失败:', error)
  } finally {
    formLoading.value = false
  }
}
/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  // dialogTitle.value = t('action.' + type)
  // formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await ProductCategoryApi.getProductCategory(id)
    } finally {
      formLoading.value = false
    }
  }
  // await getProductCategoryTree()
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗
/** 展开/折叠操作 */

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    planId: undefined,
    date: undefined,
    code: undefined,
    sort: undefined,
    status: CommonStatusEnum.ENABLE
  }
  formRef.value?.resetFields()
}
</script>

<style scoped>
.preview-title {
  margin: 20px 0 10px;
  font-weight: bold;
  color: #606266;
}
</style>
